package window;



import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

import java.awt.Color;
import java.awt.Font;


import javax.swing.JTextField;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import dao.*;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


public class Register {

	public JFrame RegisterFrame;
	private JTextField username;
	private JPasswordField password;

	/**
	 * Launch the application.
	 */

	/**
	 * Create the application.
	 */
	public Register() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		RegisterFrame = new JFrame();
		RegisterFrame.setResizable(false);
		RegisterFrame.setTitle("注册");
		ImageIcon icon=new ImageIcon("D:\\eclipse\\workspace\\TeacherInformationManagementSystem\\src\\window\\1.png");
		RegisterFrame.setIconImage(icon.getImage());
		RegisterFrame.setBounds(500, 200, 450, 300);
		RegisterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		RegisterFrame.getContentPane().setLayout(null);
		
		JLabel label = new JLabel("教师信息管理系统");
		label.setFont(new Font("幼圆", Font.BOLD, 20));
		label.setForeground(Color.DARK_GRAY);
		label.setBounds(132, 10, 180, 43);
		RegisterFrame.getContentPane().add(label);
		
		JLabel label_1 = new JLabel("密码：");
		label_1.setForeground(Color.DARK_GRAY);
		label_1.setFont(new Font("幼圆", Font.BOLD, 15));
		label_1.setBounds(98, 127, 48, 43);
		RegisterFrame.getContentPane().add(label_1);
		
		JLabel label_2 = new JLabel("账号：");
		label_2.setForeground(Color.DARK_GRAY);
		label_2.setFont(new Font("幼圆", Font.BOLD, 15));
		label_2.setBounds(98, 74, 48, 43);
		RegisterFrame.getContentPane().add(label_2);
		
		username = new JTextField();
		username.setDocument(new MyDocument(11));
		JButton register = new JButton("注册");
		username.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER)
					register.doClick();
			}
		});
		/*username.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent event) {
				char keyChar = event.getKeyChar();
				if (keyChar >= '0' && keyChar <= '9') return ;
				Toolkit.getDefaultToolkit().beep(); //声音提示
				event.consume();
			}
		});*/

		
		username.setBounds(156, 85, 165, 21);
		RegisterFrame.getContentPane().add(username);
		username.setColumns(10);
		
		password = new JPasswordField();
		password.setDocument(new MyDocument(11));
		password.addKeyListener(new KeyAdapter() {
		/*	@Override
			public void keyTyped(KeyEvent event) {
				char keyChar = event.getKeyChar();
				if (!(keyChar >= '\u4E00' && keyChar <= '\u9FA5')) return ;
				Toolkit.getDefaultToolkit().beep(); //声音提示
				event.consume();
			}*/
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER)
					register.doClick();
			}
		});
		password.setColumns(10);
		password.setBounds(156, 138, 165, 21);
		RegisterFrame.getContentPane().add(password);
		
		
		register.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(username.getText().length()==0) JOptionPane.showMessageDialog(null, "账号为空！", "error！", JOptionPane.ERROR_MESSAGE);		
				else if(String.valueOf(password.getPassword()).equals("")) JOptionPane.showMessageDialog(null, "密码为空！", "error！", JOptionPane.ERROR_MESSAGE);
				else {
					if(!TeacherDao.checkRegister(username.getText())){
					TeacherDao.regist(username.getText(),String.valueOf(password.getPassword()));
					JOptionPane.showMessageDialog(null, "注册成功！", "success!", JOptionPane.PLAIN_MESSAGE);
					Login lg=new Login();
					lg.LoginFrame.setVisible(true);
					RegisterFrame.setVisible(false);
					}
					else JOptionPane.showMessageDialog(null, "账号已被注册！", "error！", JOptionPane.ERROR_MESSAGE);		
				}
			}
		});
		register.setFont(new Font("幼圆", Font.BOLD, 12));
		register.setForeground(Color.DARK_GRAY);
		register.setBounds(228, 199, 93, 23);
		RegisterFrame.getContentPane().add(register);
		
		JButton button = new JButton("返回");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Login lg=new Login();
				lg.LoginFrame.setVisible(true);
				RegisterFrame.dispose();
			}
		});
		button.setForeground(Color.DARK_GRAY);
		button.setFont(new Font("幼圆", Font.BOLD, 12));
		button.setBounds(98, 198, 93, 23);
		RegisterFrame.getContentPane().add(button);
	}
}
